package com.rxh.risk;

import com.rxh.cache.RiskBusinessCache;
import com.rxh.cache.RiskIssuerCache;
import com.rxh.cache.RiskRefuseCache;
import com.rxh.cache.SysConstantCache;
import com.rxh.exception.PayException;
import com.rxh.pojo.risk.RiskBusiness;
import com.rxh.pojo.risk.RiskRefuse;
import com.rxh.pojo.sys.SysConstant;
import com.rxh.utils.JsonUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 王德明
 * Date: 2018/9/10
 * Time: 14:14
 * Project: Management_new
 * Package: com.rxh.risk
 */
@Component
public class RefuseComponent implements RiskInterface {

    @Autowired
    private SysConstantCache sysConstantCache;

    @Autowired
    private RiskRefuseCache riskRefuseCache;

    @Autowired
    private RiskIssuerCache riskIssuerCache;

    @Autowired
    private RiskBusinessCache riskBusinessCache;


    @Override
    public void analysis(RiskContext riskContext) throws PayException {
        String ip = riskContext.getTradeObject().getIp();
        Integer acquireId = riskContext.getCoreAcquirer().getId();
        String cardNo = riskContext.getTradeObject().getCardNo();
        String payType = riskContext.getTradeObject().getPayType();
        String issue = riskContext.getTradeObject().getIssue();
        List<SysConstant> SysConstantStopList = sysConstantCache.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.RISK_STOPS);
        List<RiskRefuse> RiskRefuseList = riskRefuseCache.getRefuse(riskContext.getMerchantWebSite().getId(), riskContext.getTradeObject().getMerNo(), acquireId, SysConstantStopList);
        for (RiskRefuse riskRefuse : RiskRefuseList) {
            //发卡行拒绝
            if (StringUtils.equals(SystemConstant.STOP_ISSUER, riskRefuse.getElement()) && StringUtils.equals(riskIssuerCache.getRiskIssuerById(Integer.parseInt(riskRefuse.getElementValue())).getName(), issue)) {
                throw new PayException("发卡行阻止: " + riskRefuse.getElementValue(), 2002);
            }
            //商户卡拒绝
            if (StringUtils.equals(SystemConstant.STOP_BUSINESS, riskRefuse.getElement()) && riskRefuse.getElementValue().equals("true") && (StringUtils.equals(riskContext.getTradeObject().getPayMode(), "Credit") || StringUtils.equals(riskContext.getTradeObject().getPayMode(), "Debit"))) {
                List<RiskBusiness> RiskBusinessList = riskBusinessCache.getBusiness(cardNo,payType,riskContext.getTradeObject().getPayMode());
                if (RiskBusinessList.size() > 0) {
                    throw new PayException("商务卡阻止: " + JsonUtils.objectToJson(RiskBusinessList),2002);
                }
            }
            //国家拒绝
            if (StringUtils.equals(SystemConstant.STOP_COUNTRY, riskRefuse.getElement()) && StringUtils.equalsIgnoreCase(riskRefuse.getElementValue(), riskContext.getTradeObject().getBillCountry())) {
                    throw new PayException("国家阻止: " + riskRefuse.getElementValue(),2002);
            }
            //IP拒绝
            if (StringUtils.equals(SystemConstant.STOP_IP, riskRefuse.getElement())) {
                if (StringUtils.equalsIgnoreCase(riskRefuse.getElementValue(), ip)) {
                    throw new PayException("IP阻止: " + riskRefuse.getElementValue(), 2002);
                } else {
                        if (riskRefuse.getElementValue().substring(0, riskRefuse.getElementValue().lastIndexOf(".")).equals(ip.substring(0, ip.lastIndexOf(".")))) {
                            String[] range = riskRefuse.getElementValue().substring(riskRefuse.getElementValue().lastIndexOf(".") + 1).split("/");
                            if (Integer.parseInt(range[0]) <= Integer.parseInt(ip.substring(ip.lastIndexOf(".") + 1)) && Integer.parseInt(range[1]) >= Integer.parseInt(ip.substring(ip.lastIndexOf(".") + 1))) {
                                throw new PayException("IP阻止: " + riskRefuse.getElementValue() + "，" + ip,2002);
                            }
                        }
                }
            }
            //邮箱拒绝
            if (StringUtils.equals(SystemConstant.STOP_EMAIL, riskRefuse.getElement()) && StringUtils.equalsIgnoreCase(riskRefuse.getElementValue(), riskContext.getTradeObject().getBillEmail())) {
                throw new PayException("邮箱阻止: " + riskRefuse.getElementValue(),2002);
            }
            //卡号拒绝
            if (StringUtils.equals(SystemConstant.STOP_CARDID, riskRefuse.getElement()) && StringUtils.equalsIgnoreCase(riskRefuse.getElementValue(), riskContext.getTradeObject().getCardNo())) {
                throw new PayException("卡号阻止: " + riskRefuse.getElementValue(),2002);
            }
        }
    }

    @Override
    public String getRiskName() {
        return SystemConstant.RISK_REFUSE;
    }

}
